package com.qqtech.qquan.homemaking.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.homemaking.dao.HomemakingBaseDao;
import com.qqtech.qquan.homemaking.dao.HomemakingTimeDao;
import com.qqtech.qquan.homemaking.enums.HomemakingBaseConst;
import com.qqtech.qquan.homemaking.enums.HomemakingTimeTypeDictEnum;
import com.qqtech.qquan.homemaking.model.HomemakingBaseVo;
import com.qqtech.qquan.homemaking.model.HomemakingTime;
import com.qqtech.qquan.homemaking.model.HomemakingTimeQuery;
import com.qqtech.qquan.homemaking.model.HomemakingTimeVo;

@Service
public class HomemakingTimeServiceImpl extends BaseServiceImpl<HomemakingTime>
		implements HomemakingTimeService {
	public static final Logger log = LoggerFactory.getLogger(HomemakingTimeServiceImpl.class);
	
	@Resource
	private HomemakingTimeDao homemakingTimeDao;
	
	@Resource
	private HomemakingBaseDao homemakingBaseDao;

	@Override
	protected BaseDao<HomemakingTime> getBaseDao() {
		return homemakingTimeDao;
	}

	@Override
	public List<HomemakingTimeVo> findByDate(Integer baseId,
			String serviceDateStr, Integer type) {
		if (baseId == null || StringUtil.isBlank(serviceDateStr)) {
			return null;
		}
		HomemakingTimeQuery q = new HomemakingTimeQuery();
		q.setBaseId(baseId);
		q.setServiceDateStr(serviceDateStr);
		q.setType(type);
		return homemakingTimeDao.queryList(q);
	}

	@Override
	public boolean checkHomemakingTime(Integer baseId, String serviceDateStr,
			String serviceHourStr) {
		HomemakingTimeQuery q = new HomemakingTimeQuery();
		q.setBaseId(baseId);
		q.setServiceDateStr(serviceDateStr);
		q.setServiceHourStr(serviceHourStr);
		q.setType(HomemakingTimeTypeDictEnum.ORDERED.getCode());
		int count = homemakingTimeDao.queryCount(q);
		return count > 0;
	}

	@Override
	public int deleteByOrderNo(String orderNo) {
		if (StringUtil.isNotBlank(orderNo)) {
			HomemakingTimeQuery q = new HomemakingTimeQuery();
			q.setOrderNo(orderNo);
			return homemakingTimeDao.delete(q);
		}
		return 0;
	}

	@Override
	public Result addRestTime(Integer baseId, String serviceDateStr, List<HomemakingTime> list) {
		Result result = new Result();
		if (baseId == null || StringUtil.isBlank(serviceDateStr)) {
			result.addErr("参数为空");
		}
		if (result.checkForm()) {
			HomemakingBaseVo homemakingBaseVo = homemakingBaseDao.getById(baseId);
			HomemakingTime dele = new HomemakingTime();
			dele.setBaseId(baseId);
			dele.setOrderNo("");
			dele.setServiceDateStr(serviceDateStr);
			if (homemakingBaseVo.getTypeId().intValue() == HomemakingBaseConst.RI_CHANG_BAO_JIE_ID) {//日常保洁是上班时间
				dele.setType(HomemakingTimeTypeDictEnum.WORK.getCode());
			} else {
				dele.setType(HomemakingTimeTypeDictEnum.REST.getCode());
			}
			homemakingTimeDao.delete(dele);
			if (list != null && !list.isEmpty()) {
				String msg = "";
				for (HomemakingTime ht : list) {
					try {
						homemakingTimeDao.insert(ht);
					} catch (Exception e) {
						msg += ht.getServiceHourStr() + " ";
					}
				}
				if (!msg.equals("")) {
					msg += "已保存或者已被预约";
				}
				result.put("msg", msg);
			}
		}
		return result;
	}
	
}